package com.zyf.wjy.dao;

import com.fasterxml.jackson.core.type.TypeReference;
import com.zyf.wjy.entity.User;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户DAO
 */
@Repository
public class UserDao extends BaseDao<User> {
    
    @Override
    protected String getFileName() {
        return "users.json";
    }
    
    @Override
    protected TypeReference<List<User>> getTypeReference() {
        return new TypeReference<List<User>>() {};
    }
    
    @Override
    protected Long getId(User entity) {
        return entity.getId();
    }
    
    @Override
    protected void setId(User entity, Long id) {
        entity.setId(id);
    }
    
    /**
     * 根据用户名查询
     */
    public User findByUsername(String username) {
        return findAll().stream()
            .filter(user -> username.equals(user.getUsername()))
            .findFirst()
            .orElse(null);
    }
    
    /**
     * 根据角色查询
     */
    public List<User> findByRole(String role) {
        return findAll().stream()
            .filter(user -> role.equals(user.getRole()))
            .toList();
    }
}

